new Vue({
    el: '#app',
    data() {
        const Pass_Word = (rule, value, callback) => {
            if (validatePassword(this.form.Pass_Word)) {
              callback(new Error(validatePassword(this.form.Pass_Word)));
            } else {
              callback();
            }
        }
        const User_Number = (rule, value, callback) => {
            if (!validatemobile(this.form.User_Number)) {
              callback(new Error('请输入有效手机号'));
            } else {
              callback();
            }
        }
        
        return {
            form: {
                User_Number:'',
                ID_Name: '',//表名
                Pass_Word: 'Aa123456',//描述
                Name:'',
                Image:'',
                Sex:'0',//0男1女
                Mail:'',
                Creation_Datetime:'',
                Role_SerialNumber:[],
                Row_Status:'0',
                Children:{
                    base_system_user_role:[],
                }
            },
            rules: {
                User_Number: [{ required: true, validator:User_Number, trigger: 'blur' }],
                ID_Name: [{ required: true, message: '请输入姓名', trigger: 'blur' }],
                Pass_Word: [{ required: true, validator:Pass_Word, trigger: 'blur' }],
                Name: [{ required: true, message: '请输入用户名称', trigger: 'blur' }],
                Role_SerialNumber: [{ required: true, message: '请选择角色名称', trigger: 'change' }],
            },
            Status:[{
                label:'是',
                value:'0'
            },{ 
                label:'否',
                value:'2'
            }],
            Role:[],
            loading: false,
        }
    },
    mounted() {
        const User_Number = sessionStorage.getItem("User_Number")
        this.getData(User_Number);
    },
    methods: {
        getData(DocumentSerialNumber){
            const that = this;
            $.ajax({
                headers:requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalGetDataStructure`,
                type: 'POST',
                data: { 
                    DocumentSerialNumber,
                    Data:  JSON.stringify({ base_system_user_info: { base_system_user_role: {} } }),
                    Condition:"Row_Status<>'1'",
                },
                success: function (res) {
                    if (res.base_system_user_info.length==1) {
                        res.base_system_user_info[0].Role_SerialNumber = [];
                        res.base_system_user_info[0].Children.base_system_user_role.forEach(item=>{
                            res.base_system_user_info[0].Role_SerialNumber.push(item.Role_SerialNumber);
                        })
                        that.form = res.base_system_user_info[0];
                    }
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
            $.ajax({
                headers:requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalQueryByTableName`,
                type: 'POST',
                data: {
                    Data: "base_system_role_info",
                    Condition:"Row_Status = '0' and Role_SerialNumber <> 'supplier'"
                },
                success: function (data) {
                    console.log(data);
                    that.Role = data;
                },
                complete: function (XMLHttpRequest) {
                    that.loading = false
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            })
        },
        submitForm(formName) {
            const that = this
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    // that.loading = true;
                    //筛出删除的部门
                    that.form.Children.base_system_user_role.forEach(item => {
                        if (!that.form.Role_SerialNumber.some(i=>i.Role_SerialNumber == item.Role_SerialNumber)) {
                            item.Row_Status = '1';
                        }
                    })
                    //更新新部门
                    that.form.Role_SerialNumber.forEach(Role_SerialNumber => {
                        if (!that.form.Children.base_system_user_role.some(i => i.Role_SerialNumber == Role_SerialNumber)) {
                            that.form.Children.base_system_user_role.push({
                                System_User_Role_SerialNumber:'',
                                User_Number:that.form.User_Number,
                                Role_SerialNumber
                            })
                        } else {
                            that.form.Children.base_system_user_role.filter(i => i.Role_SerialNumber == Role_SerialNumber)[0].Row_Status = '0';
                        }
                    })
                    const { add, modify } = that.deal();
                    let data = {}
                    if (add == null) {
                        data = { Data: JSON.stringify({ Modify: { base_system_user_info: [modify] } }) }
                    } else {
                        data = {
                            Data: JSON.stringify({ Modify: { base_system_user_info: [modify] }, Add: { base_system_user_info: [add] } }),
                        }
                    }
                    $.ajax({
                        headers:requestHeaders(),
                        url: `${BASE_URL2}/generalBusiness/generalSave`,
                        type: 'POST',
                        data,
                        success: function (res) {
                            if (res.Status === 'success') {
                                that.$message.success("提交成功")
                                window.history.go(-1)
                            } else {
                                that.$message.error("提交失败，请稍后再试")
                            }
                        },
                        complete: function (XMLHttpRequest) {
                            that.loading = false
                            requestPretreatment(XMLHttpRequest);
                        },
                        dataType: "json"
                    })
                } else {
                    that.$message({
                        message: sumberError,
                        type: 'warning'
                    })
                    return false;
                }
            });
        },
        deal() {
            const add = deepClone(this.form)
            const modify = deepClone(this.form)

            let add_base_system_user_role = [], modify_base_system_user_role = []

            for (const i in modify.Children.base_system_user_role) {
                const obj = modify.Children.base_system_user_role[i];
                if (!Object.keys(obj).some(item => item.includes('_Original'))) {
                    add_base_system_user_role.push(obj)
                } else {
                    modify_base_system_user_role.push(obj)
                }
            }

            add.Children.base_system_user_role = add_base_system_user_role
            modify.Children.base_system_user_role = modify_base_system_user_role

            if (add_base_system_user_role.length == 0) {
                return { add: null, modify };
            }
            return { add, modify };
        },
        resetForm(formName) {
            this.$refs[formName].resetFields();
        },
    }
})